<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-input placeholder="单号" title="请输入单号进行模糊匹配" v-model="pageForm.no"
                      @keyup.enter.native="doSearch" size="medium">
            </el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select
                    v-model="pageForm.userId"
                    filterable
                    remote
                    reserve-keyword
                    placeholder="请输入邮箱或用户昵称"
                    :remote-method="getUser"
                    :loading="loading">
                <el-option
                        v-for="item in options"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id">
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select
                    v-model="pageForm.fromCurrencyId"
                    filterable
                    remote
                    reserve-keyword
                    placeholder="提现币种"
                    :loading="loading">
                <el-option
                        v-for="item in currencies"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select
                    v-model="pageForm.toCurrencyId"
                    filterable
                    remote
                    reserve-keyword
                    placeholder="转入币种"
                    :loading="loading">
                <el-option
                        v-for="item in currencies"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                </el-option>
            </el-select>
        </div>

        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <div class="btn-group tool-button mt5">
            <el-checkbox v-model="isInside">不统计内部账号</el-checkbox>
        </div>

        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd">
                <i class="ti-plus"></i>
                兑换
            </el-button>
        </div>
    </el-row>

    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                :summary-method="getSummaries"
                border
                show-summary
                header-align="center"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                :default-sort="{prop: 'opAt', order: 'descending'}">
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column label="单号" header-align="left" prop="no"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="用户名" header-align="left" prop="userName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="昵称" header-align="left" prop="nickName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="邮箱" header-align="left" prop="email"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="操作详情" header-align="left"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.fromAmount}}<span style="color: #00c1de">{{scope.row.fromCurrencyName}}</span>
                    =>
                    {{scope.row.toAmount}}<span style="color: #00c1de">{{scope.row.toCurrencyName}}</span>
                </template>
            </el-table-column>

            <el-table-column sortable label="创建时间" header-align="center" align="center" prop="ctAt">
                <template slot-scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50,100]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <el-dialog
            title="兑换"
            :visible.sync="addDialogVisible"
            width="30%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="sourceCurrencyId" label="源币种">
                <el-select
                        v-model="formData.sourceCurrencyId"
                        filterable
                        reserve-keyword
                        placeholder="选择币种"
                        style="width:300px"
                        :loading="loading">
                    <el-option
                            v-for="item in currencies"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                            :disabled="!item.id">
                    </el-option>
                </el-select>

            </el-form-item>

            <el-form-item prop="targetCurrencyId" label="目标币种">
                <el-select
                        v-model="formData.targetCurrencyId"
                        filterable
                        reserve-keyword
                        placeholder="选择币种"
                        style="width:300px"
                        :loading="loading">
                    <el-option
                            v-for="item in currencies"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                            :disabled="!item.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item prop="toUser" label="用户">
                <el-select
                        v-model="formData.toUser"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="按用户名精确搜索"
                        :remote-method="getAccounts"
                        :loading="loading"
                        style="width:300px">
                    <el-option
                            v-for="item in userOptions"
                            :key="item.userId"
                            :label="item.label"
                            :value="item.userId">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item prop="amount">
                <template slot="label">
                    <span style="position:relative">
                        <span>金额</span>
                        <el-tooltip style="position:absolute;" effect="light" placement="top"
                                    content="使用京都泰国代付,金额需高于100低于10000">
                            <i class="el-icon-question" class="tips-icon"></i>
                        </el-tooltip>
                    </span>
                </template>
                <el-input maxlength="100"
                          style="width:300px"
                          placeholder="金额"
                          v-model="formData.amount"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAdd">提 交</el-button>
                  </span>
    </el-dialog>
</div>
<script>

    new Vue({
        el: "#app",
        data: function () {
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                searchDate: [],
                sum: 0,
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    fromCurrencyId: null,
                    toCurrencyId: null,
                    userId: null,
                    userCnd: "",
                    searchDate: "",

                },
                tableData: [],
                handleSelectionChange: null,
                currencies: [],
                options: [],
                loading: false,
                // 兑换窗口
                addDialogVisible: false,
                // 兑换窗口验证
                formRules: {
                    toUser: [
                        {required: true, message: '选择用户', trigger: 'blur'}
                    ],
                    amount: [
                        {required: true, message: '输入金额', trigger: 'blur'}
                    ],
                    sourceCurrencyId: [
                        {required: true, message: '选择源币种', trigger: 'blur'}
                    ],
                    targetCurrencyId: [
                        {required: true, message: '选择目标币种', trigger: 'blur'}
                    ]
                },
                formData: {
                    id: "",
                    // 源币种
                    sourceCurrencyId: "",
                    // 目标币种
                    targetCurrencyId: "",
                    // 用户id
                    toUser: '',
                    // 金额
                    amount: ''
                },
                userOptions: [],
                isInside: true,
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                self.pageForm.userCnd = "";
                if (self.isInside) {
                    if (self.pageForm.userCnd === "") {
                        self.pageForm.userCnd = "isInside";
                    } else {
                        self.pageForm.userCnd = self.pageForm.userCnd + ",isInside";
                    }
                }
                $.post(base + "/platform/user/account/exchange/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.pagination.list;
                        self.sum = data.data.sum;
                        self.pageForm.totalCount = data.data.pagination.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            getSummaries(param) {
                const {columns, data} = param;
                const sums = [];
                sums[4] = '操作金额合计USD';
                sums[5] = this.sum.fromAmount;
                return sums;
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            getUser: function (str) {
                var _this = this;
                $.post(base + "/platform/user/manager/list", {key: str}, function (data) {
                    _this.options = data.code === 0 ? data.data.map(ite => {
                            // ite.label =ite.nickName+'#<span style="color: darkblue">'+ite.email+'</span>$<span style="color: darkred">'+ite.balance+'</span>'
                            ite.label = ite.userName + '(真名：' + (ite.realName ? ite.realName : '未设置') + ')';
                            return ite
                        }) :
                        [];
                }, "json");
            },
            currencyList: function () {
                var self = this;
                $.post(base + "/platform/sys/currency/list", function (data) {
                    if (data.code == 0) {
                        self.currencies = data.data;
                        self.currencies.unshift({id:null,name:"选择 出|入 币种"})
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json")
            },


            // 打开提现窗口事件
            openAdd: function () {
                var self = this;
                self.addDialogVisible = true;
                self.formData = {};
                if (self.$refs["addForm"])
                    self.$refs["addForm"].resetFields();
            },


            // 精确搜索用户
            getAccounts: function (str) {
                var _this = this;
                if (!_this.formData.sourceCurrencyId) {
                    _this.$message({
                        message: "请选择币种",
                        type: 'error'
                    });
                    return false;
                }
                $.post(base + "/platform/user/manager/searchAccount", {
                    key: str,
                    currencyId: _this.formData.sourceCurrencyId
                }, function (data) {
                    if (data.code == 0) {
                        _this.userOptions = data.data.map(ite => {
                            ite.label = ite.userName + '(真名:' + (ite.realName ? ite.realName : '未设置,') + '余额:' + ite.balance + ")";
                            return ite
                        })
                        ;
                    } else {
                    }
                }, "json");
            },

            // 提交‘提现’申请
            doAdd: function () {
                var self = this;
                var url = base + "/platform/user/account/exchange/exchange";

                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
        },
        created: function () {
            this.pageData();
            this.currencyList();
        }
    });
</script>
<!--#
}
#-->